﻿using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tools
{
    public partial class PdfGetImage : UserControl
    {
        public PdfGetImage()
        {
            InitializeComponent();
            Dictionary<String, String> keys = new Dictionary<String, String>();
            FileStream fileStream = new FileStream("pdfGetImage.data", FileMode.OpenOrCreate);
            if (fileStream.Length > 0)
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();

                keys = binaryFormatter.Deserialize(fileStream) as Dictionary<String, String>;

                string pdfPathTextBoxText = "";
                keys.TryGetValue("pdfPathTextBoxText",out pdfPathTextBoxText);
                pdfPathTextBox.Text = pdfPathTextBoxText;

                string imagePathTextBoxText = "";
                keys.TryGetValue("imagePathTextBoxText", out imagePathTextBoxText);
                imagePathTextBox.Text = imagePathTextBoxText;
            }
            fileStream.Close();
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            //创建PdfDocument对象
            PdfDocument pdf = new PdfDocument();
            //加载PDF文档
           
           if (Directory.Exists(pdfPathTextBox.Text))
            {
                string[] files = Directory.GetFiles(pdfPathTextBox.Text);
                foreach (string file in files)
                {
                    string fileName = Path.GetFileName(file);
                    Console.WriteLine(file); // 输出文件路径
                    if (file.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)) {
                        pdf.LoadFromFile(file);
                        int i = 1;
                        //循环遍历文档中的所有页面
                        foreach (PdfPageBase page in pdf.Pages)
                        {
                            //从每个页面提取图像并将其保存到指定的文件路径
                            foreach (Image image in page.ExtractImages())
                            {

                                string imageFileName = fileName.Replace(".pdf", "") + ".png";
                                image.Save(@""+ imagePathTextBox.Text+"/" + imageFileName, System.Drawing.Imaging.ImageFormat.Png);
                                i++;
                                pdf.Dispose();
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("文件夹不存在");
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            pdfPathTextBox.Text = null;
            imagePathTextBox.Text = null;
        }

        private void saveConfigButton_Click(object sender, EventArgs e)
        {
            Dictionary<String, String> keys = new Dictionary<String, String>();

            FileStream fileStream = new FileStream("pdfGetImage.data", FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            keys.Add("pdfPathTextBoxText", this.pdfPathTextBox.Text);
            keys.Add("imagePathTextBoxText", this.imagePathTextBox.Text);

            binaryFormatter.Serialize(fileStream, keys);

            fileStream.Close();
        }
    }
}
